package com.ap.core.bulkupload;

import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.ap.core.bulkupload.provider.BulkFileUploadServiceProvider;
import com.ap.core.bulkupload.types.BulkFileUploadEnum;
import com.ap.core.context.ApplicationContextProvider;
import com.ap.core.exception.APCoreException;

public class BulkFileUploadMain {
	
	/** The Constant LOG. */
	private static final Logger LOG = LoggerFactory.getLogger(BulkFileUploadMain.class);
	
	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		
		String methodName = "main() -";
		BulkFileUploadServiceProvider fileUploadServiceProvider = null;
		AbstractApplicationContext context = null;
		String xmlFile = "";
		try {
			LOG.info(methodName + "BulkFileUploadMain processing...");

			if(args.length == 1) {
				LOG.error("Please provide bulk upload input file as an argument ...");
				throw new APCoreException("Please provide bulk upload input file as an argument ...");
			}

			xmlFile = args[1];
			LOG.info("Bulk Upload Input File Path ["+xmlFile+"]");
			BulkFileUploadEnum bulkUploadType = BulkFileUploadEnum.MERCHANT;
			
			/* Load spring application context */
			context = new ClassPathXmlApplicationContext("/spring/app-context.xml");
			fileUploadServiceProvider = (BulkFileUploadServiceProvider)ApplicationContextProvider.getBean(BulkFileUploadServiceProvider.class);
			LOG.info(methodName+ "Invoking BulkFileUploadServiceProvider");
			String outputFilePath = fileUploadServiceProvider.process(bulkUploadType, xmlFile);
			LOG.info(methodName+ "BulkFileUploadMain executed successfully and created output file as ["+outputFilePath+"]");
		} catch (APCoreException e) {
			LOG.error(methodName+ "Exception occurred ["+Arrays.toString(e.getStackTrace())+"]");
			e.printStackTrace();		
		} catch(Exception e) {
			LOG.error(methodName+ "Exception occurred ["+Arrays.toString(e.getStackTrace())+"]");
			e.printStackTrace();
		}
		finally {
			LOG.info(methodName+ "Closing application context");
			if(context != null) {
				context.close();
			}
		}
	}
}
